﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Journal.Common.Extensions;

namespace Journal.Common.Helpers.HtmlControls
{
    public static class ButtonControls
    {
        public static MvcHtmlString Button(string buttonText)
        {
            return new MvcHtmlString(BaseButton(buttonText).ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString Button(string buttonText, IDictionary<string, string> dataParams)
        {
            TagBuilder button = BaseButton(buttonText);
            button.InsertDataAttributes(dataParams);
            return new MvcHtmlString(button.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString Button(string buttonText, object htmlAttributes)
        {
            TagBuilder button = BaseButton(buttonText);
            if (htmlAttributes != null)
            {
                button.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            }

            return new MvcHtmlString(button.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString Button(string buttonText, object htmlAttributes, IDictionary<string, string> dataParams)
        {
            TagBuilder button = BaseButton(buttonText);
            if (htmlAttributes != null)
            {
                button.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            }

            button.InsertDataAttributes(dataParams);
            return new MvcHtmlString(button.ToString(TagRenderMode.Normal));
        }

        private static TagBuilder BaseButton(string value)
        {
            TagBuilder button = new TagBuilder("button");
            button.InnerHtml += value;
            return button;
        }
    }
}